const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    var checkIDCard = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证号不能为空'));
      }
      setTimeout(() => {
        if (!ID_Validity(value)) {
          callback(new Error('请输入正确的身份证号'));
        }
      }, 100);
    }
    return {
      pickerOptions: pickerOptions,
      rules: {
        ID_Number: [{
          validator: checkIDCard,
          trigger: 'blur'
        }]
      },
      form: {     //采购价格单
        Purchase_unitprice_SerialNumber: "",
        Start_Date: '',
        End_Date: '',
        Supplier_SerialNumber: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
        Children: {
          purchase_unitprice_detail: [{     //采购价格单明细
            Purchase_Unit_Price_Detail_SerialNumber: "",
            Purchase_Unit_Price_SerialNumber: '',
            Article_SerialNumber: '',
            Unit_Price: '',
            Purchase_Unit_Price: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    const EnterpriseInfo_SerialNumber = sessionStorage.getItem("EnterpriseInfo_SerialNumber")
    this.getData(EnterpriseInfo_SerialNumber)
  },
  methods: {
    getData(EnterpriseInfo_SerialNumber) {
      const _this = this
      $.ajax({
        //	url: '../getContractContentByContract_SerialNumberAction',
        url: '../getEnterpriseInfoByEnterpriseInfo_SerialNumberAction',
        type: 'POST',
        data: { Data: EnterpriseInfo_SerialNumber, token: $.cookie("token") },
        success: function (res) {
          console.log('根据企业编号获取结果', res)
          _this.form = res.enterprise_info[0]
          if (!_this.form.Children.enterprise_legal_person || _this.form.Children.enterprise_legal_person.length == 0) {
            _this.form.Children.enterprise_legal_person = [{
              Enterprise_Legal_Person_SerialNumber: "",
              Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
              Legal_Person: '',
              Mobile: '',
              ID_Number: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
          if (!_this.form.Children.series || _this.form.Children.series.length == 0) {
            _this.form.Children.contract_receiving_info = [{
              series_SerialNumber: "",
              Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
              Address: '',
              Mobile: '',
              Bank_Name: '',
              Bank_Account: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Modify: JSON.stringify({ enterprise_info: [modify] }) }
          } else {
            data = {
              Modify: JSON.stringify({ enterprise_info: [modify] }),
              Add: JSON.stringify({ enterprise_info: [add] })
            }
          }
          _this.loading = true
          $.ajax({
            url: '../modifyGeneralBusinessAction',
            type: 'POST',
            data: data,
            success: function (res) {
              console.log('提交结果', res)
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.history.go(-1)
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const add = deepClone(this.form)
      const modify = deepClone(this.form)
      let add_enterprise_legal_person = [], modify_enterprise_legal_person = []
      let add_series = [], modify_series = []
      for (const i in modify.Children.enterprise_legal_person) {
        const obj = modify.Children.enterprise_legal_person[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_enterprise_legal_person.push(obj)
        } else {
          modify_enterprise_legal_person.push(obj)
        }
      }
      for (const i in modify.Children.series) {
        const obj = modify.Children.series[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_series.push(obj)
        } else {
          modify_series.push(obj)
        }
      }

      add.Children.enterprise_legal_person = add_enterprise_legal_person
      add.Children.series = add_series

      modify.Children.enterprise_legal_person = modify_enterprise_legal_person
      modify.Children.series = modify_series

      if (add_enterprise_legal_person.length == 0 && add_series.length == 0) {
        return { add: null, modify };
      }
      return { add, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // add_barcode() {
    //   this.form.Children.barcode.push({       //条形码
    //     BarCode: "",
    //     Article_SerialNumber: '',
    //     Row_Status: '',
    //     Operation_ID: '',
    //     Operation_Datetime: '',
    //   })
    // },
    // remove_barcode() {
    //   const length = this.form.Children.barcode.length
    //   console.log(length)
    //   if (length > 1) {
    //     if (this.form.Children.barcode[length - 1].Enterprise_Info_SerialNumber) {
    //       for (let i = length - 1; i > 0; --i) {
    //         if (this.form.Children.barcode[i].Row_Status === '0') {
    //           this.form.Children.barcode[i].Row_Status = '1'
    //           break;
    //         }
    //       }
    //     } else {
    //       this.form.Children.barcode.pop()
    //     }
    //   } else {
    //     this.$message({
    //       message: '不能在删除了',
    //       type: 'warning'
    //     })
    //   }
    //   if (this.form.Children.barcode.length > 1) {
    //     this.form.Children.barcode.pop()
    //   } else {
    //     this.$message({
    //       message: '不能在删除了',
    //       type: 'warning'
    //     })
    //   }
    // },
    // add_purchase_unitprice() {
    //   this.form.Children.purchase_unitprice.push({     //采购价格单
    //     Purchase_unitprice_SerialNumber: "",
    //     Start_Date: '',
    //     End_Date: '',
    //     Supplier_SerialNumber: '',
    //     Row_Status: '',
    //     Operation_ID: '',
    //     Operation_Datetime: '',
    //   })
    // },
    // remove_purchase_unitprice() {
    //   const length = this.form.Children.purchase_unitprice.length
    //   console.log(length)
    //   if (length > 1) {
    //     if (this.form.Children.purchase_unitprice[length - 1].Enterprise_Info_SerialNumber) {
    //       for (let i = length - 1; i > 0; --i) {
    //         if (this.form.Children.purchase_unitprice[i].Row_Status === '0') {
    //           this.form.Children.purchase_unitprice[i].Row_Status = '1'
    //           break;
    //         }
    //       }
    //     } else {
    //       this.form.Children.purchase_unitprice.pop()
    //     }
    //   } else {
    //     this.$message({
    //       message: '不能在删除了',
    //       type: 'warning'
    //     })
    //   }
    //   if (this.form.Children.purchase_unitprice.length > 1) {
    //     this.form.Children.purchase_unitprice.pop()
    //   } else {
    //     this.$message({
    //       message: '不能在删除了',
    //       type: 'warning'
    //     })
    //   }
    // },
    // add_series() {
    //   this.form.Children.series.push({     //商品类型
    //     Series_SerialNumber: "",
    //     Series_Name: '',
    //     Row_Status: '',
    //     Operation_ID: '',
    //     Operation_Datetime: '',
    //   })
    // },
    // remove_series() {
    //   const length = this.form.Children.series.length
    //   console.log(length)
    //   if (length > 1) {
    //     if (this.form.Children.series[length - 1].Enterprise_Info_SerialNumber) {
    //       for (let i = length - 1; i > 0; --i) {
    //         if (this.form.Children.series[i].Row_Status === '0') {
    //           this.form.Children.series[i].Row_Status = '1'
    //           break;
    //         }
    //       }
    //     } else {
    //       this.form.Children.series.pop()
    //     }
    //   } else {
    //     this.$message({
    //       message: '不能在删除了',
    //       type: 'warning'
    //     })
    //   }
    //   if (this.form.Children.series.length > 1) {
    //     this.form.Children.series.pop()
    //   } else {
    //     this.$message({
    //       message: '不能在删除了',
    //       type: 'warning'
    //     })
    //   }
    // },
  }
})